﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BobcatWiiLib
{
    /// <summary>
    /// This control shows the current IR points for one wiimote in the wiimote's
    /// own camera coordinates. This can help you set up the calibration, as you can 
    /// see what the camera can see and what it can't see.
    /// </summary>
    public partial class WiimoteCameraViewerControl : UserControl
    {
        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public WiimoteCameraViewerControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// You pass in the WiimoteWrapper object that we will be showing the points for.
        /// </summary>
        public void initialize(WiimoteWrapper wiimoteWrapper)
        {
            m_WiimoteWrapper = wiimoteWrapper;
            wiimoteWrapper.IRStatusChangedEvent += onWiimoteUpdate;
            wiimoteWrapper.IRPositionChangedEvent += onWiimoteUpdate;
        }

        /// <summary>
        /// Closes down the control and unregisters from wiimote events.
        /// </summary>
        public void close()
        {
            m_WiimoteWrapper.IRStatusChangedEvent -= onWiimoteUpdate;
            m_WiimoteWrapper.IRPositionChangedEvent -= onWiimoteUpdate;
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Called when we get an update to the wiimote's state or position.
        /// </summary>
        void onWiimoteUpdate(object sender, WiimoteWrapper.IRPointArgs e)
        {
            // We find the position of the active points, store them and trigger
            // a redraw of the control...
            m_ActivePoints = m_WiimoteWrapper.getActivePoints();
            Invalidate();
        }

        /// <summary>
        /// We draw the control, including the set of visible points.
        /// </summary>
        private void WiimoteCameraViewerControl_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            // We draw a grid...
            for (int iX = 0; iX <= Width; iX += GRID_SPACING)
            {
                g.DrawLine(Pens.LightGreen, iX, 0, iX, Height);
            }
            g.DrawLine(Pens.LightGreen, Width, 0, Width, Height);

            for (int iY = 0; iY <= Width; iY += GRID_SPACING)
            {
                g.DrawLine(Pens.LightGreen, 0, iY, Width, iY);
            }
            g.DrawLine(Pens.LightGreen, 0, Height, Width, Height);

            // We get the size of this control as doubles to help with scaling the
            // wiimote image to the size of ths control...
            double dWidth = Width;
            double dHeight = Height;

            // We draw the active points...
            foreach (WiimoteWrapper.IRPointArgs pi in m_ActivePoints)
            {
                // We need to convert the point into points relative to our control...
                double dX = pi.PositionX / WIIMOTE_CAMERA_WIDTH * dWidth;
                double dY = pi.PositionY / WIIMOTE_CAMERA_HEIGHT * dHeight;
                g.FillEllipse(Brushes.Red, (float)(dX - 2), (float)(dY - 2), 4.0f, 4.0f);
            }
        }

        #endregion

        #region Private data

        // Constants...
        private const int WIIMOTE_CAMERA_WIDTH = 1024;
        private const int WIIMOTE_CAMERA_HEIGHT = 768;
        private const int GRID_SPACING = 20;

        // The wiimote that we will show IR points for...
        private WiimoteWrapper m_WiimoteWrapper;

        // The collection of active (i.e. currently visible) points...
        private IList<WiimoteWrapper.IRPointArgs> m_ActivePoints = new List<WiimoteWrapper.IRPointArgs>();

        #endregion
    }
}
